﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using Xenta.Attributes;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Api
{
    [Dependency("52e475b9-a783-489a-8382-c8f34da5366b", 
        Lifestyle = DependencyLifestyle.Singleton)]
    public class GZipCompressTransform : IHandler
    {
        public void Handle(ref string mimeType,
            Dictionary<String, String> inProperties,
            Stream inStream,
            Dictionary<String, String> outProperties,
            Stream outStream)
        {
            using(var gzipStream = new GZipStream(outStream, CompressionMode.Compress, true))
                inStream.CopyTo(gzipStream);
            outProperties["ContentMimeType"] = mimeType;
            mimeType = "application/gzip";
        }
    }

    [Dependency("d15452ad-1e6f-492a-b2e2-dbce11bcb4ec",
        Lifestyle = DependencyLifestyle.Singleton)]
    public class ImageResizeTransform : IHandler
    {
        public void Handle(ref string mimeType,
            Dictionary<String, String> inProperties,
            Stream inStream,
            Dictionary<String, String> outProperties,
            Stream outStream)
        {
            if(!MimeTypeHelper.IsImage(mimeType))
                throw Err.Of(this).NotValid("mimeType").Ex();

            if(inProperties.ContainsKey("Size"))
            {
                int size = inProperties["Size"].ParseAs(0);
                if(size == 0)
                    throw Err.Of(this).NotValid("Size").Ex();
                ImageHelper.Resize(inStream, outStream, size);
            }
            else if(inProperties.ContainsKey("Width") && inProperties.ContainsKey("Height"))
            {
                int width = inProperties["Width"].ParseAs(0);
                if(width == 0)
                    throw Err.Of(this).NotValid("Width").Ex();

                int height = inProperties["Height"].ParseAs(0);
                if(height == 0)
                    throw Err.Of(this).NotValid("Height").Ex();

                ImageHelper.Resize(inStream, outStream, width, height);
            }
            else
                throw Err.Of(this).NotValid("inProperties").Ex();
        }
    }

    [Dependency("f6f3b677-d9b5-4873-a164-47d535a49337",
        Lifestyle = DependencyLifestyle.Singleton)]
    public class ImageWatermarkTransform : IHandler
    {
        public void Handle(ref string mimeType,
            Dictionary<String, String> inProperties,
            Stream inStream,
            Dictionary<String, String> outProperties,
            Stream outStream)
        {
            if(!MimeTypeHelper.IsImage(mimeType))
                throw Err.Of(this).NotValid("mimeType").Ex();
            if(!inProperties.ContainsKey("Text"))
                throw Err.Of(this).NotSpecified("Text").Ex();

            string text = inProperties["Text"];
            if(String.IsNullOrWhiteSpace(text))
                throw Err.Of(this).NotValid("Text").Ex();

            string position = inProperties["Position"];
            int padding = inProperties.ContainsKey("Padding")
                ? inProperties["Padding"].ParseAs<Int32>()
                : 0;
            string bgClrArgb = inProperties.ContainsKey("BgClrArgb") ?
                inProperties["BgClrArgb"]
                : String.Empty;
            string fgClrArgb = inProperties.ContainsKey("FgClrArgb") ?
                inProperties["FgClrArgb"]
                : String.Empty;
            string fontFamily = inProperties.ContainsKey("FontFamily") ?
                inProperties["FontFamily"]
                : String.Empty;
            float fontSize = inProperties.ContainsKey("FontSize")
                ? inProperties["FontSize"].ParseAs<Single>()
                : 0;

            ImageHelper.Watermark(inStream, 
                outStream, 
                text,
                position,
                padding,
                bgClrArgb,
                fgClrArgb,
                fontFamily,
                fontSize);
        }
    }
}
